Tweak UI for warnings and errors
authorAlex Crichton <alex@alexcrichton.com>
Sun, 13 Mar 2016 22:24:55 +0000 (15:24 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Thu, 17 Mar 2016 06:42:49 +0000 (23:42 -0700)
Right now Cargo prints out errors justified like all other status messages, but
this can look odd without coloration:

```
   error some error message
```

Instead, this commit changes both warnings and errors to use the same style of:

```
error: some error message
warning: some warning message
```

Additionally, warnings now only print out "warning" in yellow instead of the
entire message (like errors do)

src/cargo/core/shell.rs
src/cargo/lib.rs
src/cargo/ops/cargo_package.rs
src/cargo/util/flock.rs
src/cargo/util/toml.rs
tests/support/mod.rs
tests/test_bad_config.rs
tests/test_cargo_compile.rs
tests/test_cargo_concurrent.rs
tests/test_cargo_install.rs

index 2dbbc3f04a7abd63561140078c51be5b2bf157c6..57876c97188402ec9015691c9393cd97ecb742c3 100644 (file)
@@ -73,7 +73,7 @@ impl MultiShell {
     {
         match self.verbosity {
             Quiet => Ok(()),
-            _ => self.out().say_status(status, message, GREEN)
+            _ => self.out().say_status(status, message, GREEN, true)
         }
     }
 
@@ -95,12 +95,12 @@ impl MultiShell {
         }
     }
 
-    pub fn error<T: ToString>(&mut self, message: T) -> CargoResult<()> {
-        self.err().say_status("error", message.to_string(), RED)
+    pub fn error<T: fmt::Display>(&mut self, message: T) -> CargoResult<()> {
+        self.err().say_status("error:", message, RED, false)
     }
 
-    pub fn warn<T: ToString>(&mut self, message: T) -> CargoResult<()> {
-        self.err().say(message, YELLOW)
+    pub fn warn<T: fmt::Display>(&mut self, message: T) -> CargoResult<()> {
+        self.err().say_status("warning:", message, YELLOW, false)
     }
 
     pub fn set_verbosity(&mut self, verbose: bool, quiet: bool) -> CargoResult<()> {
@@ -179,14 +179,22 @@ impl Shell {
         Ok(())
     }
 
-    pub fn say_status<T, U>(&mut self, status: T, message: U, color: Color)
+    pub fn say_status<T, U>(&mut self,
+                            status: T,
+                            message: U,
+                            color: Color,
+                            justified: bool)
                             -> CargoResult<()>
         where T: fmt::Display, U: fmt::Display
     {
         try!(self.reset());
         if color != BLACK { try!(self.fg(color)); }
         if self.supports_attr(Attr::Bold) { try!(self.attr(Attr::Bold)); }
-        try!(write!(self, "{:>12}", status.to_string()));
+        if justified {
+            try!(write!(self, "{:>12}", status.to_string()));
+        } else {
+            try!(write!(self, "{}", status));
+        }
         try!(self.reset());
         try!(write!(self, " {}\n", message));
         try!(self.flush());
index 7d98028fe8f18cfd286328dad2024643975e551f..2d5db07fa11d7acbea2ff24895ed56239908a8ce 100644 (file)
@@ -183,12 +183,12 @@ pub fn shell(verbosity: Verbosity, color_config: ColorConfig) -> MultiShell {
 // and for others, e.g. docopt version info, print to stdout.
 fn output(err: String, shell: &mut MultiShell, fatal: bool) {
     let (std_shell, color, message) = if fatal {
-        (shell.err(), RED, Some("error"))
+        (shell.err(), RED, Some("error:"))
     } else {
         (shell.out(), BLACK, None)
     };
     let _ = match message{
-        Some(text) => std_shell.say_status(text, err.to_string(), color),
+        Some(text) => std_shell.say_status(text, err.to_string(), color, false),
         None => std_shell.say(err, color)
     };
 }
@@ -201,7 +201,8 @@ pub fn handle_error(err: CliError, shell: &mut MultiShell) {
 
     let hide = unknown && shell.get_verbose() != Verbose;
     if hide {
-        let _ = shell.err().say_status("error", "An unknown error occurred", RED);
+        let _ = shell.err().say_status("error:", "An unknown error occurred",
+                                       RED, false);
     } else {
         output(error.to_string(), shell, fatal);
     }
index bf173f1642cbbfe02efd8c801276693df753c29b..277b88eae1b84eadc4c8c71c6b4a2dceebeb568a 100644 (file)
@@ -93,7 +93,7 @@ fn check_metadata(pkg: &Package, config: &Config) -> CargoResult<()> {
         things.push_str(&missing.last().unwrap());
 
         try!(config.shell().warn(
-            &format!("warning: manifest has no {things}. \
+            &format!("manifest has no {things}. \
                     See http://doc.crates.io/manifest.html#package-metadata for more info.",
                     things = things)))
     }
index 238dc8a18a4e41340a278ce57184a015dae008be..d40978dbc7cba8cff6a67aa5488a3b8fc69eb76a 100644 (file)
@@ -250,7 +250,7 @@ fn acquire(config: &Config,
         }
     }
     let msg = format!("waiting for file lock on {}", msg);
-    try!(config.shell().err().say_status("Blocking", &msg, CYAN));
+    try!(config.shell().err().say_status("Blocking", &msg, CYAN, true));
 
     block().chain_error(|| {
         human(format!("failed to lock file: {}", path.display()))
index 98c9aa1e546599b1cb4f7f0d91b9accb52ec1cac..17f043cab15022a9a69beed9b0cc6ff080bddf53 100644 (file)
@@ -568,8 +568,8 @@ impl TomlManifest {
                                          profiles,
                                          publish);
         if project.license_file.is_some() && project.license.is_some() {
-            manifest.add_warning(format!("warning: only one of `license` or \
-                                                   `license-file` is necessary"));
+            manifest.add_warning(format!("only one of `license` or \
+                                          `license-file` is necessary"));
         }
         for warning in warnings {
             manifest.add_warning(warning.clone());
@@ -680,7 +680,7 @@ fn process_dependencies(cx: &mut Context,
 
         if details.version.is_none() && details.path.is_none() &&
            details.git.is_none() {
-            cx.warnings.push(format!("warning: dependency ({}) specified \
+            cx.warnings.push(format!("dependency ({}) specified \
                                       without providing a local path, Git \
                                       repository, or version to use. This will \
                                       be considered an error in future \
@@ -839,7 +839,7 @@ fn normalize(lib: &Option<TomlLibTarget>,
                 kinds.iter().filter_map(|s| {
                     let kind = LibKind::from_str(s);
                     if let Err(ref error) = kind {
-                        warnings.push(format!("warning: {}", error))
+                        warnings.push(error.to_string());
                     }
                     kind.ok()
                 }).collect()
index d25a4651e7c3d550c9e3bad37356a938cf7a5f93..9a55c1f53cdb303f65de6bd38cbd1d40ff87a081 100644 (file)
@@ -659,7 +659,7 @@ pub fn path2url(p: PathBuf) -> Url {
 
 pub static RUNNING:     &'static str = "     Running";
 pub static COMPILING:   &'static str = "   Compiling";
-pub static ERROR:       &'static str = "       error";
+pub static ERROR:       &'static str = "error:";
 pub static DOCUMENTING: &'static str = " Documenting";
 pub static FRESH:       &'static str = "       Fresh";
 pub static UPDATING:    &'static str = "    Updating";
index 32428e41771b68f3ccf5b7b7f22b067426dab187..afc91e8f65dcff63c2d79e7ed27f95cb36a01ccc 100644 (file)
@@ -398,7 +398,7 @@ test!(unused_keys {
 
     assert_that(foo.cargo_process("build"),
                 execs().with_status(0).with_stderr("\
-unused manifest key: target.foo.bar
+warning: unused manifest key: target.foo.bar
 "));
 });
 
index 731546e6372ea5cfb358a315cfa9f04d6bc7ad94..9270e83c230a45388d84c7962ef19cf61277d0ac 100644 (file)
@@ -811,7 +811,9 @@ test!(unused_keys {
         "#);
     assert_that(p.cargo_process("build"),
                 execs().with_status(0)
-                       .with_stderr("unused manifest key: project.bulid\n"));
+                       .with_stderr("\
+warning: unused manifest key: project.bulid
+"));
 
     let mut p = project("bar");
     p = p
@@ -832,7 +834,9 @@ test!(unused_keys {
         "#);
     assert_that(p.cargo_process("build"),
                 execs().with_status(0)
-                       .with_stderr("unused manifest key: lib.build\n"));
+                       .with_stderr("\
+warning: unused manifest key: lib.build
+"));
 });
 
 test!(self_dependency {
index 3f8b7324038738526926a8bf6110736c27c36f2f..6b4a5b6a0ca6e5d4a8c7ae586af935e9260903ec 100644 (file)
@@ -87,7 +87,7 @@ test!(one_install_should_be_bad {
 {error} binary `foo[..]` already exists in destination as part of `[..]`
 ", error = ERROR)));
     assert_that(good, execs().with_status(0).with_stderr("\
-be sure to add `[..]` to your PATH [..]
+warning: be sure to add `[..]` to your PATH [..]
 "));
 
     assert_that(cargo_home(), has_installed_exe("foo"));
index b30dd5fc166bae094123e9085879b8c99dc54c73..59270d11771bc0891ad9ff70e5c6347e1a0c4dd3 100644 (file)
@@ -579,7 +579,7 @@ test!(do_not_rebuilds_on_local_install {
                 execs().with_status(0).with_stdout(&format!("\
 {installing} [..]
 ", installing = INSTALLING)).with_stderr("\
-be sure to add `[..]` to your PATH to be able to run the installed binaries
+warning: be sure to add `[..]` to your PATH to be able to run the installed binaries
 "));
 
     assert!(p.build_dir().c_exists());